package com.jb.minidvd;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Pattern;

public class DVDMgr
{
	/**
	 * 创建DVD对象
	 */
	public Scanner getScanner()
	{
		Scanner scanner = new Scanner(System.in);
		return scanner;
	}

	public void returnStartMenu() throws Exception
	{
		System.out.println("***********************");
		System.out.println("0、  返回主菜单");
		System.out.println("***********************");
		while (true)
		{
			int choose = getScanner().nextInt();
			if (choose != 0)
			{
				System.out.println("输入错误，请正确输入：0。");
			}
			else
			{
				startMenu();
			}
		}

	}

	DVDSet dvd = new DVDSet();

	/**
	 * 初始三个DVD
	 */
	public void initial()
	{

		dvd.dvdInfos[0] = new DVDInfo("罗马假日", 3.68F);
		dvd.dvdInfos[1] = new DVDInfo("1", 3.68F);
		dvd.dvdInfos[2] = new DVDInfo("2", 3.68F);
		dvd.dvdInfos[3] = new DVDInfo("3", 3.68F);
		dvd.dvdInfos[4] = new DVDInfo("4", 3.68F);
		dvd.dvdInfos[5] = new DVDInfo("5", 3.68F);
	}

	/**
	 * 开始菜单
	 * 
	 * @author ZhangXin
	 * @throws Exception
	 * @email sanjin6035@hotmail.com
	 */
	public void startMenu() throws Exception
	{
		System.out.println("欢迎使用迷你DVD管理器");
		System.out.println("***********************");
		System.out.println("1、 新增DVD");
		System.out.println("2、 查看DVD");
		System.out.println("3、 删除DVD");
		System.out.println("4、 借出DVD");
		System.out.println("5、 归还DVD");
		System.out.println("6、 查看排行榜");
		System.out.println("0、 退           出");
		System.out.println("***********************");
		System.out.println("请选择：");
		boolean flag = true;
		while (flag)
		{
			try
			{
				int choose = getScanner().nextInt();
				switch (choose)
				{
				case 1:
					flag = false;
					add();
					break;
				case 2:
					flag = false;
					search();
					break;
				case 3:
					flag = false;
					delete();
					break;
				case 4:
					flag = false;
					lend();
					break;
				case 5:
					returnDvd();
					break;
				case 6:
					list();
					break;
				case 0:
					exit();
					break;

				default:
					System.out.println("请正确输入数字进行选择。请选择：");
					break;
				}
			}
			catch (Exception e)
			{
				System.err.println("只能输入数字。");
			}
		}

	}

	/**
	 * 返回主菜单
	 * 
	 * @author WangSen
	 * @email 623096767@qq.com
	 * @throws Exception
	 */
	public void returnMain() throws Exception
	{
		startMenu();
	}

	/**
	 * 查看DVD
	 * 
	 * @author QinQi
	 * @email 13594340840@163.com
	 * @throws Exception
	 */
	public void search() throws Exception
	{
		System.out.println("--------查询结果--------");
		System.out.println("序号\t借出状态\t借出日期\t名称");
		for (int i = 0; i < dvd.dvdInfos.length; i++)
		{
			if (dvd.dvdInfos[i] != null)
			{
				System.out.println((i + 1)
						+ "\t"
						+ (dvd.dvdInfos[i].state == 1 ? "未借出" : "已借出")
						+ "\t"
						+ (dvd.dvdInfos[i].lendDate == null ? "暂未借出"
								: new SimpleDateFormat("yyyy-MM-dd")
										.format(dvd.dvdInfos[i].lendDate))
						+ "\t" + "《" + dvd.dvdInfos[i].name + "》");
			}
			else
			{

				break;
			}
		}
		returnStartMenu();
	}

	/**
	 * 新增DVD
	 * 
	 * @throws Exception
	 */
	public void add() throws Exception
	{
		System.out.println("--------添加DVD--------");
		boolean flag = true;
		while (flag)
		{
			int j = 0;
			String name;
			float oncePrice;
			while (true)
			{
				System.out.println("请输入DVD名称；");
				name = getScanner().next().trim();
				for (int i = 0; i < dvd.dvdInfos.length; i++)
				{
					if (dvd.dvdInfos[i] != null
							&& name.equals(dvd.dvdInfos[i].name))
					{
						j++;
					}
				}
				if (j == 0)
				{
					try
					{
						System.out.println("请输入租借单价；");
						oncePrice = getScanner().nextFloat();
						for (int i = 0; i < dvd.dvdInfos.length; i++)
						{
							if (dvd.dvdInfos[i] == null)
							{
								// System.out.println(i);
								dvd.dvdInfos[i] = new DVDInfo(name, oncePrice);
								// System.out.println(dvd.dvdInfos[i]);
								System.out.println("《" + dvd.dvdInfos[i].name
										+ "》添加成功，租借每天"
										+ dvd.dvdInfos[i].oncePrice + "元");
								break;
							}
						}
						System.out.println("是否继续添加？  1：是   0：否");
						if (getScanner().nextInt() == 0)
						{
							flag = false;
						}
					}
					catch (Exception e)
					{
						System.err.println("只能输入Float型");
					}

				}
				else
				{
					System.err.println("这个DVD已经存在请重新添加。");
					add();
				}
				returnStartMenu();
			}
		}
	}

	/**
	 * 删除DVD
	 */
	public void delete()
	{
		System.out.println("--------删除DVD--------");
		String delBook = getScanner().nextLine().trim();
		try
		{
			for (int i = 0; i < dvd.dvdInfos.length; i++)
			{
				if (dvd.dvdInfos[i].getName().equals(delBook))
				{
					for (int j = i + 1; j < dvd.dvdInfos.length; j++)
					{
						dvd.dvdInfos[i++] = dvd.dvdInfos[j];
					}
					System.out.println("删除成功");
					returnStartMenu();
				}
			}
		}
		catch (Exception e)
		{
			System.err.println("请确定你要删除的DVD是否存在。");
		}
	}

	/**
	 * 借出DVD
	 * 
	 * @author RanTingTing
	 * @email 1831767382@qq.com
	 * @throws Exception
	 */
	public void lend() throws Exception
	{
		System.out.println("--------借出DVD--------");
		int j = 0;
		String searchName = null;
		String date = null;
		while (true)
		{
			System.out.println("请输入你要借的DVD名称：");
			searchName = getScanner().next().trim();
			for (int i = 0; i < dvd.dvdInfos.length; i++)
			{
				if (dvd.dvdInfos[i] != null)
				{
					if (dvd.dvdInfos[i].name.equals(searchName))
					{
						j++;
						if (dvd.dvdInfos[i].state == 0)
						{
							System.err
									.println("这个DVD已经被借出，无法借阅。请检查输入的DVD名字是否有误。");
							lend();
						}
						dvd.dvdInfos[i].setState(0);
						while (true)
						{
							System.out.println("请输入你要借出日期：（yyyy-MM-dd）");
							date = getScanner().next();
							String pattern = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";

							boolean r = Pattern.matches(pattern, date);
							if (r == false)
							{
								System.err.println("您输入的日期格式有误请重新输入");
							}
							else
							{
								System.out.println("《" + dvd.dvdInfos[i].name
										+ "》借出成功。");
								SimpleDateFormat sdf = new SimpleDateFormat(
										"yyyy-MM-dd");
								dvd.dvdInfos[i].setLendDate(sdf.parse(date));
								(dvd.dvdInfos[i].count)++;
								break;
							}

						}
					}
				}
				else
				{
					break;
				}
			}
			if (j == 0)
			{
				System.out.println("这个DVD不存在，无法借出。");
			}
			returnStartMenu();
		}
	}

	/**
	 * 归还DVD并计算租金
	 * 
	 * @throws Exception
	 */
	public void returnDvd() throws Exception
	{
		System.out.println("--------归还DVD--------");
		int j = 0;
		String searchName = null;
		String date = null;
		while (true)
		{
			System.out.println("请输入你要归还的DVD名称：");
			searchName = getScanner().next().trim();
			for (int i = 0; i < dvd.dvdInfos.length; i++)
			{
				if (dvd.dvdInfos[i] != null
						&& dvd.dvdInfos[i].name.equals(searchName))
				{
					j++;
					if (dvd.dvdInfos[i].state == 1)
					{
						System.err.println("这个DVD已经被归还，无需归还。请检查输入的DVD名字是否有误。");
						returnDvd();
					}
					while (true)
					{

						System.out.println("请输入你要归还日期：（yyyy-MM-dd）");
						date = getScanner().next();
						String pattern = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";

						boolean r = Pattern.matches(pattern, date);
						if (r == false)
						{
							System.err.println("您输入的日期格式有误请重新输入");
						}
						else
						{
							SimpleDateFormat sdf = new SimpleDateFormat(
									"yyyy-MM-dd");
							dvd.dvdInfos[i].setReturnDate(sdf.parse(date));
						}
						if ((dvd.dvdInfos[i].returnDate
								.compareTo(dvd.dvdInfos[i].lendDate)) < 0)
						{
							// System.out.println(dvd.dvdInfos[i].lendDate
							// + "........." + dvd.dvdInfos[i].returnDate);
							// System.out.println(dvd.dvdInfos[i].returnDate
							// .compareTo(dvd.dvdInfos[i].lendDate));
							System.err.println("归还时间不能早于借出时间，请重新输入归还时间：");
						}
						else
						{
							dvd.dvdInfos[i].setState(1);
							float money = (charge(dvd.dvdInfos[i].lendDate,
									dvd.dvdInfos[i].returnDate))
									* dvd.dvdInfos[i].getOncePrice();
							System.out.println("《" + dvd.dvdInfos[i].name
									+ "》归还成功，应该缴纳的租金为：" + money + "元");
							break;
						}
					}
				}
				// else
				// {
				// break;
				// }
			}
			if (j == 0)
			{
				System.out.println("这个DVD不存在，无法归还。");
			}
			returnStartMenu();
		}
	}

	/**
	 * 计算日期差
	 * 
	 * @param dstr1
	 *            第一个日期
	 * @param dstr2
	 *            第二个日期
	 * 
	 */
	public long charge(Date dstr1, Date dstr2)
	{
		long diff = dstr2.getTime() - dstr1.getTime();
		return (diff / (1000 * 60 * 60 * 24));

	}

	/**
	 * 借出排行榜
	 * 
	 * @throws Exception
	 */
	public void list() throws Exception
	{
		System.out.println("--------查看排行榜--------");
		DVDInfo di = new DVDInfo("k", 6);
		for (int i = 0; i < dvd.dvdInfos.length; i++)
		{
			if (dvd.dvdInfos[i] != null)
			{

				for (int j = i; j < dvd.dvdInfos.length - 1; j++)
				{
					if (dvd.dvdInfos[j] != null)
					{

						if (dvd.dvdInfos[i].getCount() < dvd.dvdInfos[j]
								.getCount())
						{
							di = dvd.dvdInfos[i];
							dvd.dvdInfos[i] = dvd.dvdInfos[j];
							dvd.dvdInfos[j] = di;
						}

					}
				}
			}
		}

		System.out.println("DVD名称\t租借次数");
		for (int i = 0; i < dvd.dvdInfos.length; i++)
		{
			if (dvd.dvdInfos[i] != null)
			{
				System.out.print(dvd.dvdInfos[i].getName());
				System.out.println("\t" + dvd.dvdInfos[i].getCount());
			}
		}
		returnStartMenu();
	}

	public void exit()
	{
		System.out.println("***********************");
		System.out.println("谢谢使用");
		System.out.println("***********************");
		System.exit(0);
	}
}
